/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pvtracejava;

/**
 *
 * @author Administrator
 */
public class Until {

    public static int hex2decimal(String s) {
        String tmpValue = s.toUpperCase();
        String x0 = tmpValue.substring(0, 2);
        if (x0.compareToIgnoreCase("0x") ==0) {
           tmpValue = tmpValue.replaceAll("0X", "");
        }
        String digits = "0123456789ABCDEF";

        int val = 0;
        for (int i = 0; i < tmpValue.length(); i++) {
            char c = tmpValue.charAt(i);
            int d = digits.indexOf(c);
            val = 16 * val + d;
        }
        return val;
    }

    // precondition:  d is a nonnegative integer
    public static String decimal2hex(int d) {
        String digits = "0123456789ABCDEF";
        if (d == 0) {
            return "0";
        }
        String hex = "";
        while (d > 0) {
            int digit = d % 16;                // rightmost digit
            hex = digits.charAt(digit) + hex;  // string concatenation
            d = d / 16;
        }
        return hex;
    }
}
